/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.x3.purchasing.inquery;

import com.x3.purchasing.common.ClassApplicationModule;
import com.x3.purchasing.quotation.Quotation;
import com.x3.purchasing.quotation.QuotationDAO;
import com.x3.purchasing.quotation.QuotationDAOImpl;
import java.text.SimpleDateFormat;
import java.util.List;
import org.zkoss.zhtml.Filedownload;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Label;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.Toolbarbutton;

/**
 *
 * @author kris
 */
public class InqueryDetailOperatorWnd extends ClassApplicationModule {

    private QuotationDAO quotationDAO;
    private InqueryDAO inqueryDAO;
    private Label lblJudul;
    private Label lblKeterangan;
    private Label lblTanggalBuka;
    private Label lblTanggalTutup;
    private Session session;
    private Grid gridPenawaran;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

    public void onCreate() throws Exception {
        quotationDAO = new QuotationDAOImpl();
        inqueryDAO = new InqueryDAOImpl();
        lblJudul = (Label) getFellow("lblJudul");
        lblKeterangan = (Label) getFellow("lblKeterangan");
        lblTanggalBuka = (Label) getFellow("lblTanggalBuka");
        lblTanggalTutup = (Label) getFellow("lblTanggalTutup");
        gridPenawaran = (Grid) getFellow("gridPenawaran");
        this.loadDetailInquery();
    }

    private void loadDetailInquery() throws Exception {
        session = Sessions.getCurrent();
        List<Quotation> quotations = quotationDAO.getByInquery((Integer) session.getAttribute("inqueryId"));
        Inquery inquery = inqueryDAO.get((Integer) session.getAttribute("inqueryId"));
        lblJudul.setValue(inquery.getJudul());
        lblKeterangan.setValue(inquery.getKeterangan());
        lblTanggalBuka.setValue(dateFormat.format(inquery.getTanggalBuka()));
        lblTanggalTutup.setValue(dateFormat.format(inquery.getTanggalTutup()));
        int index = 1;
        if (quotations.size() > 0) {
            Rows rows = new Rows();
            for (final Quotation quotation : quotations) {
                Row row = new Row();
                row.appendChild(new Label(index + ""));
                row.appendChild(new Label(quotation.getVendor().getNama()));
                Toolbarbutton btnPenawaran = new Toolbarbutton();
                btnPenawaran.setStyle("font-size:10px;color:#0000FF;");
                btnPenawaran.setLabel("[Download Penawaran]");
                btnPenawaran.addEventListener("onClick", new EventListener() {

                    public void onEvent(Event event) throws Exception {
                        if (quotation.getPenawaran() != null) {
                            Filedownload.save(quotation.getPenawaran(), "application/pdf", quotation.getNamaFile());
                        }
                    }
                });
                row.appendChild(btnPenawaran);
                rows.appendChild(row);
                index++;
            }
            gridPenawaran.appendChild(rows);
        }
    }
}
